package com.mmall.controller.portal;

import com.mmall.common.Const;
import com.mmall.common.ResponseCode;
import com.mmall.common.ServerResponse;
import com.mmall.pojo.Shipping;
import com.mmall.pojo.User;
import com.mmall.service.IShippingService;
import com.mmall.service.impl.ShippingServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * Create by lisy on Qingdao
 **/
@Controller
@RequestMapping("shipping")
public class ShippingController {

    @Autowired
    private IShippingService iShippingService;
    /**
     * ####1.添加地址
     * /shipping/add.do
     * http://localhost:8080/shipping/add.do?userId=1
     * &receiverName=geely
     * &receiverPhone=010
     * &receiverMobile=18688888888
     * &receiverProvince=%E5%8C%97%E4%BA%AC
     * &receiverCity=%E5%8C%97%E4%BA%AC%E5%B8%82
     * &receiverAddress=%E4%B8%AD%E5%85%B3%E6%9D%91
     * &receiverZip=100000
     */
    @RequestMapping("add.do")
    @ResponseBody
    public ServerResponse add(HttpSession session, Shipping shipping){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            return ServerResponse.createByErrorMssage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), ResponseCode.ILLEGAL_ARGUMENT.getDesc());
        }
        return iShippingService.add(user.getId(), shipping);
    }

    /**
     * ####2.删除地址
     * /shipping/del.do**
     */
    @RequestMapping("del.do")
    @ResponseBody
    public ServerResponse del(HttpSession session, Integer shippingId){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            return ServerResponse.createByErrorMssage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), ResponseCode.ILLEGAL_ARGUMENT.getDesc());
        }
        return iShippingService.del(user.getId(), shippingId);
    }

    /**
     * ####3.登录状态更新地址
     * /shipping/update.do**
     * http://localhost:8080/shipping/update.do?id=5
     * &receiverName=AAA
     * &receiverPhone=010
     * &receiverMobile=18688888888
     * &receiverProvince=%E5%8C%97%E4%BA%AC
     * &receiverCity=%E5%8C%97%E4%BA%AC%E5%B8%82
     * &receiverDistrict=%E6%B5%B7%E6%B7%80%E5%8C%BA
     * &receiverAddress=%E4%B8%AD%E5%85%B3%E6%9D%91
     * &receiverZip=100000
     */
    @RequestMapping("update.do")
    @ResponseBody
    public ServerResponse update(HttpSession session, Shipping shipping){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            return ServerResponse.createByErrorMssage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), ResponseCode.ILLEGAL_ARGUMENT.getDesc());
        }
        return iShippingService.update(user.getId(), shipping);
    }

    /**
     * ####4.选中查看具体的地址
     * /shipping/select.do**
     */
    @RequestMapping("select.do")
    @ResponseBody
    public ServerResponse select(HttpSession session, Integer shippingId){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            return ServerResponse.createByErrorMssage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), ResponseCode.ILLEGAL_ARGUMENT.getDesc());
        }
        return iShippingService.select(user.getId(), shippingId);
    }

    /**
     * ####5.地址列表
     * /shipping/list.do**
     * http://localhost:8080/shipping/list.do
     */
    @RequestMapping("list.do")
    @ResponseBody
    public ServerResponse list(@RequestParam(value = "pageNum", defaultValue = "1")int pageNum,
                               @RequestParam(value = "pageSize", defaultValue = "10")int pageSize,
                               HttpSession session){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            return ServerResponse.createByErrorMssage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), ResponseCode.ILLEGAL_ARGUMENT.getDesc());
        }
        return iShippingService.list(user.getId(), pageNum, pageSize);
    }
}
